#!/usr/bin/env python
import pygtk
import sys, array, os
pygtk.require('2.0')

import gtk
import gnomeapplet

#import subprocess

iconpath = "/home/furyy/src/python/pavol/Speaker_Icon.svg"

class PAVolApplet(gtk.EventBox):
    
    #__gsignals__ = { "expose-event": "override" ,
    #                "screen-changed": "override" }
    
    fill = 1.0
    def __init__(self,applet):
        gtk.EventBox.__init__(self)
        
        #self.set_app_paintable(True)
        #self.set_visible_window(False)
        
        #Dunno, for transparent cairo draws
        self.picWidth,self.picHeight = 24,24
        self.image = gtk.Image()
        self.pixbuf = self.make_icon(self.picWidth,self.picHeight)
        self.image.set_from_pixbuf(self.pixbuf)
        
        self.applet = applet
        self.applet.set_background_widget(self.applet)
        
        self.applet.connect ("change-size", self.on_change_size)
        self.add(self.button)
        self.tooltips = gtk.Tooltips()
        self.tooltips.set_tip(self, "PAVolControl")
        #: %d%%"%(self.fill*100))

        self.connect("button-press-event",self.pressed)
        #self.connect("scroll-event",self.scrolled)
        #applet.connect("change_background", self.onChangeBackground)
        self.connect("size-allocate",self.on_resize)
        self.__setup_applet_menu()
        
    def on_resize(self,w,e):
        if self.picWidth != e.width or self.picHeight!=e.height:
            self.picWidth,self.picHeight = e.width,e.height
            self.image.set_from_pixbuf(self.make_icon(e.width -e.x,e.height -e.y))
            #self.draw_icon(e.width-2,e.height-2)
        pass

    def make_icon(self,Width,Height):
        return gtk.gdk.pixbuf_new_from_file_at_size(iconpath, Width,Height)

    def onChangeBackground(self, applet, type, color, pixmap):
        applet.set_style(None)
        rc_style = gtk.RcStyle()
        applet.modify_style(rc_style)
        if (type == gnomeapplet.COLOR_BACKGROUND):
            applet.modify_bg(gtk.STATE_NORMAL, color)
        elif (type == gnomeapplet.PIXMAP_BACKGROUND):
            style = applet.style
            style.bg_pixmap[gtk.STATE_NORMAL] = pixmap
            self.applet.set_style(style)
        
    def pressed(self, obj, event):
        print event.type, event.button
        if event.button == 1:
            os.system('/usr/bin/pavol')

    def on_change_size (self, applet, size):
        self.set_size_request( size, size )

    def __setup_applet_menu(self):
        self.applet.setup_menu_from_file (
            "/home/furyy/src/python/pavol", "applet-menu.xml",
            None, [("Help", lambda a,b: self.msg("Help"),),
            ("About", lambda a,b: self.msg("About"),)
            ])
            
    def msg(self,msg):
        dialog = gtk.MessageDialog(parent=None,
              flags=0,
              type=gtk.MESSAGE_WARNING,
              buttons=gtk.BUTTONS_OK,
              message_format=msg)
        dialog.connect('response', lambda w, id: dialog.destroy())
        dialog.show_all()

    def __has_svg_support (self):
        for format in gtk.gdk.pixbuf_get_formats():
            if format["name"] == "svg":
                return True
        return False

def sample_factory(applet, iid):
    #applet.add(PAVolApplet(applet))
    #self.picWidth,self.picHeight = 24,24
    ev = gtk.EventBox()
    image = gtk.Image()
    
    #pixbuf = make_icon(24,24)
    image.set_from_pixbuf(make_icon(22,22))
    ev.add(image)
    
    #button = gtk.Button()
    #button.set_relief(gtk.RELIEF_NONE)
    #button.add(image)

    applet.add(ev)
    tooltips = gtk.Tooltips()
    tooltips.set_tip(ev, "PAVolControl")
    #: %d%%"%(self.fill*100))

    ev.connect("button-press-event",showMenu,applet)
    #self.connect("scroll-event",self.scrolled)
    #applet.connect("change_background", self.onChangeBackground)
    #self.connect("size-allocate",self.on_resize)
    #self.__setup_applet_menu()
    applet.show_all()
    return True

def make_icon(Width,Height):
    return gtk.gdk.pixbuf_new_from_file_at_size(iconpath, Width,Height)


def showMenu(widget, event, applet):
    if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
        widget.emit_stop_by_name("button_press_event")
        create_menu(applet)
    if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
        os.system('/usr/bin/pavol')


def create_menu(applet):
	propxml="""
			<popup name="button3">
			<menuitem name="Item 3" verb="About" label="_About" pixtype="stock" pixname="gtk-about"/>
			</popup>"""
	verbs = [("About", showAboutDialog)]
	applet.setup_menu(propxml, verbs, None)


def pressed(self, obj, event):
    if event.button == 1:
        os.system('/usr/bin/pavol')
        
def onChangeBackground(self, applet, type, color, pixmap):
    applet.set_style(None)
    rc_style = gtk.RcStyle()
    applet.modify_style(rc_style)
    if (type == gnomeapplet.COLOR_BACKGROUND):
        applet.modify_bg(gtk.STATE_NORMAL, color)
    elif (type == gnomeapplet.PIXMAP_BACKGROUND):
        style = applet.style
        style.bg_pixmap[gtk.STATE_NORMAL] = pixmap
        self.applet.set_style(style)

if len(sys.argv) == 2:
    if sys.argv[1] == "run-in-window":
        main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        main_window.set_title("Python Applet")
        main_window.connect("destroy", gtk.main_quit)
        
        app = gnomeapplet.Applet()
        sample_factory(app, None)
        app.reparent(main_window)
        main_window.show_all()
        gtk.main()
        sys.exit()

gnomeapplet.bonobo_factory("OAFIID:GNOME_PAVolApplet_Factory",
                         gnomeapplet.Applet.__gtype__,
                         "Simple PulseAudio volume control", "1.0", sample_factory)

